{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rows by columns, or m by n\n",
      "A is (1, 3)\n",
      "B is (3, 1)\n",
      "A * B =  [[33]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "A = np.array([[3, 2, 4]])\n",
    "B = np.array([[1], [5], [5]])\n",
    "\n",
    "print(\"rows by columns, or m by n\")\n",
    "print(\"A is\", A.shape)\n",
    "print(\"B is\", B.shape)\n",
    "\n",
    "print(\"A * B = \", np.dot(A, B))    # note -> A*B does not give \n",
    "                                   # you matrix multiplication in numpy!!! "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  2.  3.  4.]]\n"
     ]
    }
   ],
   "source": [
    "# If a matrix A is multiplied by the identity matrix, the result is A.\n",
    "A = np.array([[1,2,3,4]])\n",
    "print(np.dot(A, np.eye(4)))    # equals A!!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  1.00000000e+00   0.00000000e+00]\n",
      " [  8.88178420e-16   1.00000000e+00]]\n",
      "[[1 3]\n",
      " [2 4]]\n"
     ]
    }
   ],
   "source": [
    "B = np.array([[1,2],[3,4]])\n",
    "\n",
    "print(np.dot(B, np.linalg.inv(B)))   # returns the identity matrix (approximately)\n",
    "print(B.transpose())                 # returns the transposed matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[12]\n",
      " [-3]] \n",
      "\n",
      "[[12]\n",
      " [-3]]\n"
     ]
    }
   ],
   "source": [
    "# ********************** eigenvalues and eigenvectors!!! ********************** #\n",
    "\n",
    "A = np.array([[2, -4], [-1, -1]])\n",
    "x = np.array([[4], [-1]])\n",
    "eigVal = 3\n",
    "\n",
    "print(np.dot(A, x), \"\\n\")\n",
    "print(eigVal * x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3. -2.]\n"
     ]
    }
   ],
   "source": [
    "# Now we know A has a real eigenvalue, so let's compute it with numpy.\n",
    "\n",
    "w, v = np.linalg.eig(A)\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 4. -1.]\n",
      "Our original eigenvector was [4, -1]\n"
     ]
    }
   ],
   "source": [
    "# ok, so the square matrix A has two eigenvalues, 3 and -2.\n",
    "# but what about the corresponding eigenvector?\n",
    "\n",
    "v[:, 0]       # this is the normalized eigenvector corresponding to w[0], or 3.\n",
    "\n",
    "# let's unnormalize it to see if we were right.\n",
    "import math\n",
    "length = math.sqrt(x[0]**2 + x[1]**2)  # the length of our original eigenvector x\n",
    "print(v[:, 0] * length)\n",
    "\n",
    "print(\"Our original eigenvector was [4, -1]\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Woohoo! It's important to remember that all multiples of this \n",
    "# eigenvector will be an eigenvector of A corresponding to lambda."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "det(A) =  -2.0\n"
     ]
    }
   ],
   "source": [
    "# ************************ Determinants ************************ #\n",
    "A = np.array([[1, 2], [3, 4]])\n",
    "print(\"det(A) = \", np.linalg.det(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.18149711,  0.07590154,  0.98045793],\n",
       "       [-0.65271926,  0.73643135, -0.17783826],\n",
       "       [-0.73553815, -0.6722409 , -0.08411777]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ************************ Single Value Decomposition ************************ #\n",
    "A = np.array([[1, 2, 3, 4, 5, 6, 7, 8],\n",
    "              [9,10,11,12, 4,23,45, 2],\n",
    "              [5, 3, 5, 2,56, 3, 6, 4]])\n",
    "\n",
    "U, s, V = np.linalg.svd(A)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
